import random
from system.gameServer import gameServer

class Timer:
    def __init__(self, timeout, maxTimeout = None):
        self.reset(timeout,maxTimeout)
        
    def fire(self):
        if not self.fired:
            if gameServer.time > self.endTime:
                self.fired = True
                return True
            else:
                return False
        else:
            return False
    
    def reset(self, timeout = None, maxTimeout = None):
        self.fired = False
        if timeout and maxTimeout:
            self.timeout = random.uniform(timeout, maxTimeout)
        elif timeout:
            self.timeout = timeout
        self.endTime = gameServer.time + self.timeout
        
    def getTime(self):
        if self.fired:
            return 0
        else:
            return self.timeout - (self.endTime - gameServer.time)
